<?php
namespace Home\Controller;
use Think\Controller;
class OverdueController extends CommnController{

    public function __construct(){
        parent::__construct();
    }

    /*
    * 弹框添加备注
    * */
    public function add_mark(){

         $mark= I('info');
         $pay_no=I('number');
         if(empty(trim($mark))){
             $this->ajax('1002', '信息填写完整！');
         }

         $arr=array('repayment_no'=>$pay_no,'reason'=>$mark,'name'=>$_SESSION['name'],'ctime'=>time());
         if(M('mark', '', 'KEFU')->add($arr)){
             $this->ajax('1001', '备注成功！');
         }else{
             $this->ajax('1002', '备注失败！');
         }
    }

    /*
     * 弹框备注
     * */
    public function overdue_mark(){
        $overdue_id=I('get.id');
        $data= M('user_overdue', '', 'URGE')->where(['id'=>$overdue_id])->field('repayment_no,status')->find();
        $repay_no=$data['repayment_no'];
        $this->status=$data['status'];
        $list=M('mark', '', 'KEFU')->where(['repayment_no'=>$repay_no])->field('repayment_no,reason,name,ctime')->select();
        foreach ($list as  $k=>$v){
                  if($v['ctime']>100){
                      $list[$k]['ctime']=date('Y-m-d H:i',$v['ctime']);
                  }else{
                      $list[$k]['ctime']='-';
                  }
        }

        $this->assign(array(
            'data' => $list,
            'number' => $repay_no,
            'id' => $overdue_id,
//            'tol' => $tol
        ));
        $this->display();
    }


    // 逾期列表
    public function OverdueList(){

        $this->roles = role(I('rid'));
        $this->rid=I('rid');
        $type = I('get.type');//1未还款 2 已经还款
        $search = I('get.key');
        $this->key=$search;
        $overdue = M('user_overdue', '', 'URGE');
        $row = '10';
        $where = [];

        if (!empty($type) && $type != 3) {
            $where['b.status'] = array('eq', $type);
        }
        if (!empty($search)) {
//            echo $type;die;
            $where['a.truename|a.mobile'] = array('like', '%' . $search . '%');
        }
        $where['b.merchant_num'] = array('eq', 'dh_kefu');
        $where['b.type'] = array('eq', 1);

        $tol = $overdue->join('b join dxd_user.user a on b.vid = a.vid')->where($where)->count();
        $page = new PageController($tol, $row);
        $fpage = $page->fpage();
        $field = 'b.id,a.truename,a.mobile,a.address,b.vid,b.repayment_no,b.repayment_money,b.should_time,b.overdue_day,b.overdue_money,b.overdue_time,b.status';
        $data = $overdue->join('b join dxd_user.user a on b.vid = a.vid')->order('b.overdue_time desc')->where($where)->LIMIT($page->listfirst, $page->listRows)->field($field)->select();

        foreach ($data as $k => $v) {
            $user = M('user', '', 'USER')->where(['vid' => $v['vid']])->field('truename,mobile,address,sex')->find();
            if($user['sex']==1){
                $data[$k]['sex'] = '男';
            }else if($user['sex']==2){
            $data[$k]['sex'] = '女';
            }else{
                $data[$k]['sex'] = '未知';
            }
            $data[$k]['truename'] = $user['truename'];
            $data[$k]['mobile'] = $user['mobile'];
            $data[$k]['address'] = $user['address'];
            if ($v['status'] == 1) {
                $data[$k]['status'] = '未还款';
            } else if ($v['status'] == 2) {
                $data[$k]['status'] = '已还款';
            } else if ($v['status'] == 3) {
                $data[$k]['status'] = '已续期';
            }
            $data[$k]['should_time'] = date('Y-m-d H:i', $v['should_time']);
            if (!empty($v['overdue_time'])) {
                $data[$k]['overdue_time'] = date('Y-m-d H:i', $v['overdue_time']);
            } else {
                $data[$k]['overdue_time'] = '-';
            }
            //判断是否转交过
            if ($type == 1) {
                $w = [];
                $w['repayment_no'] = array('eq', $v['repayment_no']);
                $w['status'] = array('neq', '3');
                $apply = M('overdue_change_apply', '', 'URGE')->where($w)->find();
                if (!empty($apply)) {
                    unset($data[$k]);
                }
            }
        }

        $this->assign(array(
            'data' => $data,
            'fpage' => $fpage,
            'type' => $type,
            'tol' => $tol
        ));
        $this->display();
    }

  // 逾期已经移交列表
    public function Overdue_moveList()
    {
        $type = I('get.type');//1未还款 2 已经还款
        $search = I('get.key');
        $overdue = M('user_overdue', '', 'URGE');
        $row = '12';
        $where = [];

        $tol = $overdue->where($where)->count();
        $page = new PageController($tol, $row);
        $fpage = $page->fpage();

        //已经转交的
        if ($type == 3) {
            $tol = $overdue->where($where)->count();
            $where = [];
            $where['a.status'] = array('neq', '3');
            $where['a.publish_merchant_num'] = array('eq', 'dh_kefu');

            if (!empty($search)) {
                $where['a.truename|a.mobile'] = array('like', '%' . $search . '%');
            }
            $data = $overdue->join('b join overdue_change_apply a on b.repayment_no=a.repayment_no')->where($where)->select();
        }
        foreach ($data as $k => $v) {
            $user = M('user', '', 'USER')->where(['vid' => $v['vid']])->field('truename,mobile,address,sex')->find();
            if($user['sex']==1){
                $data[$k]['sex'] = '男';
            }else if($user['sex']==2){
            $data[$k]['sex'] = '女';
            }else{
                $data[$k]['sex'] = '未知';
            }
            $data[$k]['truename'] = $user['truename'];
            $data[$k]['mobile'] = $user['mobile'];
            $data[$k]['address'] = $user['address'];
            if ($v['status'] == 1) {
                $data[$k]['status'] = '未还款';
            } else if ($v['status'] == 2) {
                $data[$k]['status'] = '已还款';
            } else if ($v['status'] == 3) {
                $data[$k]['status'] = '已续期';
            }
            $data[$k]['should_time'] = date('Y-m-d H:i', $v['should_time']);
            if (!empty($v['overdue_time'])) {
                $data[$k]['overdue_time'] = date('Y-m-d H:i', $v['overdue_time']);
            } else {
                $data[$k]['overdue_time'] = '-';
            }
            //判断是否转交过
            if ($type == 1) {
                $w = [];
                $w['repayment_no'] = array('eq', $v['repayment_no']);
                $w['status'] = array('neq', '3');
                $apply = M('overdue_change_apply', '', 'URGE')->where($w)->find();
                if (!empty($apply)) {
                    unset($data[$k]);
                }
            }
        }
        $tol=count($data);

        $this->assign(array(
            'data' => $data,
            'fpage' => $fpage,
            'type' => $type,
            'tol' => $tol
        ));
        $this->display();
    }

    // 移交列表
    public function OverdueApplyRecord()
    {
        $apply = M('overdue_change_apply', '', 'URGE');
        $row = '12';
        $where = [];
        $search = I('get.key');
        $this->key=$search;
        if (!empty($search)) {
            $where['c.truename|c.mobile'] = array('like', '%' . $search . '%');
        }
        $where['publish_merchant_num'] = array('eq', 'dh_kefu');
        $tol = $apply->join('b join urge_merchant a on b.accept_merchant_num = a.merchant_num')
        ->join('user_overdue d on b.repayment_no=d.repayment_no ')->join('dxd_user.user c on d.vid=c.vid')
        ->order('b.ctime desc')->where($where)->count();

        $page = new PageController($tol, $row);
        $fpage = $page->fpage();
        $field = 'b.repayment_no,b.ctime,b.status,a.name,c.truename,c.mobile';
        $data = $apply->join('b join urge_merchant a on b.accept_merchant_num = a.merchant_num')
            ->join('user_overdue d on b.repayment_no=d.repayment_no ')->join('dxd_user.user c on d.vid=c.vid')
            ->order('b.ctime desc')->where($where)->LIMIT($page->listfirst, $page->listRows)->field($field)->select();

        $faqi = M('urge_merchant', '', 'URGE')->where(['merchant_num' => 'dh_kefu'])->getfield('name');
        foreach ($data as $k => $v) {
            if ($v['status'] == 1) {
                $data[$k]['status'] = '待处理';
            } else if ($v['status'] == 2) {
                $data[$k]['status'] = '接收';
            } else if ($v['status'] == 3) {
                $data[$k]['status'] = '拒绝';
            } else if ($v['status'] == 3) {
                $data[$k]['status'] = '自动移交';
            }
            $data[$k]['ctime'] = date('Y-m-d H:i', $v['ctime']);

        }
        $this->assign(array(
            'data' => $data,
            'fpage' => $fpage,
            'faqi' => $faqi,
            'tol' => $tol
        ));
        $this->display();

    }



    // 转交
    public function Overdue_apply()
    {
        if (empty($_POST)) {
            $overdue = I('get.id');
            $w = [];
            $w['status'] = array('eq', '1');
            $w['merchant_num'] = array('neq', 'dh_kefu');
            //如果是第一次移交 必须选择北京新贷后
            $first= M('user_overdue', '', 'URGE')->join('b join overdue_change_apply a on b.repayment_no=a.repayment_no')->where(['b.id' => $overdue])->find();
            if(empty($first)){
                $w['merchant_num'] = array('eq', 'dh_12345678');
            }
            $merchant = M('urge_merchant', '', 'URGE')->where($w)->field('id,name')->select();

            $this->assign(array(
                'merchant' => $merchant,
                'overId' => $overdue
            ));
            $this->display();
            die;
        } else {

            $id = I('id');
            $overdue = I('overdue');
            $borrow = M('user_overdue', '', 'URGE')->where(['id' => $overdue])->field('repayment_no,overdue_day')->find();//订单号\
            if ($borrow['overdue_day'] <= 3) {
                $this->ajax('1002', '逾期不超过3天！');
            }

            //是否已经移交
            $exit = M('overdue_change_apply', '', 'URGE')->where(['repayment_no' => $borrow['repayment_no']])->find();
            if (!empty($exit)) {
                $this->ajax('1002', '转交已存在');
            }

            $accept_num = M('urge_merchant', '', 'URGE')->where(['id' => $id])->getfield('merchant_num');//贷后公司码

            $arr = array('publish_merchant_num' => 'dh_kefu', 'repayment_no' => $borrow['repayment_no'], 'ctime' => time(), 'accept_merchant_num' => $accept_num);
            $add = M('overdue_change_apply', '', 'URGE')->add($arr);
            if ($add) {
                $this->ajax('1001', '申请成功');
            } else {
                $this->ajax('1002', '申请失败');
            }
        }
    }
//    public function ajax($code, $result, $r = '')
//    {
//        if (empty($r)) {
//            $this->ajaxReturn(['code' => $code, 'result' => $result]);
//        } else {
//            $this->ajaxReturn(['code' => $code, 'result' => $result]);
//            return false;
//        }
//    }
}